#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import logging
import os
import sys
import shutil

execfile(os.path.join(sys.path[0], "framework.py"))

from unittest import TestCase, TestLoader, TextTestRunner, TestSuite
from oriolus.trotter.core import DownloadError, Fetcher, Downloader, Indexer, PacketDescriptor, PacketList
from oriolus.pith.packets.cryptography import Cryptography
from oriolus.trotter.fetchers.persistentfetcher import PersistentFetcher

logging.basicConfig(level = logging.INFO, format = "[%(levelname)s] %(message)s", )

class MockFetcher(Fetcher):
    def __init__(self, cache, timeout = 30):
        self.cache = cache

    def get(self, url, login = None, password = None):
        if url == "error.txt":
            raise DownloadError("Mock error raised!")

        filename = os.path.join(self.cache, os.path.basename(url))
        file = open(filename, "w")
        file.write("bla-bla")
        file.close()

        return file.name

class MockFetcher1(Fetcher):
    def __init__(self, cache, timeout = 30):
        self.cache = cache

    def get(self, url, login = None, password = None):
        filename = os.path.join(self.cache, os.path.basename(url))
        #print "mock file exists - %s" % os.path.exists(os.path.abspath(url))
        #print "source - %s" % os.path.abspath(url)
        #print "dest - %s" % os.path.abspath(filename)
        shutil.copy(os.path.abspath(url), filename)

        return filename

class TestCommon(TestCase):
    cache = "./testdata/cache"
    output = "./testdata/output"
    work = "./testdata/work"
    wget_path = ';' + os.path.abspath("../../etc/Windows")

    def setUp(self):
        self.env = os.getenv('PATH')
        os.putenv('PATH', self.env + self.wget_path)
        if not os.path.exists(self.output):
            os.makedirs(self.output)
        if not os.path.exists(self.work):
            os.makedirs(self.work)

    def tearDown(self):
        if os.path.exists(self.cache):
            shutil.rmtree(self.cache)

        if os.path.exists(self.output):
            shutil.rmtree(self.output)
        if os.path.exists(self.work):
            shutil.rmtree(self.work)

        if os.path.exists(os.path.abspath("./testdata/test_keyring")):
            os.remove(os.path.abspath("./testdata/test_keyring"))
            
        if os.path.exists(os.path.abspath("./testdata/test_keyring.bak")):
            os.remove(os.path.abspath("./testdata/test_keyring.bak"))
            
class TestDownloader(TestCommon):
    def setUp(self):
        TestCommon.setUp(self)

    def tearDown(self):
        TestCommon.tearDown(self)

    def test_downloader_1(self):
        original_file = "original_file.txt"

        loader = Downloader(self.cache, self.output, MockFetcher)
        file = loader.get("original_file.txt")
        self.assertEqual(original_file, os.path.basename(file))
        loader.clean()
        self.assertFalse(os.path.exists(self.cache))

    def test_downloader_2(self):
        cache = "./testdata/cache/somefile.txt"
        loader = Downloader(cache, self.output, MockFetcher)
        file = loader.get("original_file.txt")
        self.assertTrue(os.path.exists(cache))

    def test_downloader_3(self):
        loader = Downloader(self.cache, self.output, MockFetcher)
        file = loader.get("file://./testdata/_file.txt")
        self.assertTrue(os.path.exists(file))
        self.assertTrue(os.path.isfile(file))
        self.assertEqual(os.path.abspath(self.output), os.path.dirname(file))

    def test_download_error(self):
        loader = Downloader(self.cache, self.output, MockFetcher)
        self.assertRaises(DownloadError, loader.get, "error.txt")

class TestIndexer(TestCommon):
    def setUp(self):
        TestCommon.setUp(self)

    def tearDown(self):
        TestCommon.tearDown(self)

    def test_read_sources_empty(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        sources = indexer.read_sources()
        self.assertEqual([], sources)

    def test_read_sources_1(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist1.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        sources = indexer.read_sources()
        s = ["http://defaulturl.ru/bla-bla/", "http://defaulturl.com/bla-bla/", "ftp://defaulturl.ru/bla-bla/"]
        self.assertEqual(s, sources)

    def test_read_sources_2(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist2.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        sources = indexer.read_sources()
        s = []
        self.assertEqual(s, sources)

    def test_get_sources_1(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist2.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        sources = indexer.get_sources("url1, url2, url3")
        s = ["url1", "url2", "url3"]
        self.assertEqual(s, sources)

    def test_get_sources_2(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist3.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        sources = indexer.get_sources("url1, url2, url3")
        s = ["url1", "url2", "url3", "url4", "url5"]
        self.assertEqual(s, sources)

    def test_get_sources_3(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist3.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        sources = indexer.get_sources("url1,url2,url3")
        s = ["url1", "url2", "url3", "url4", "url5"]
        self.assertEqual(s, sources)

    def test_add_urls_1(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist1.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        indexer.add_urls("ftp://defaulturl1.com/bla-bla/")
        s = ["ftp://defaulturl.ru/bla-bla/", "ftp://defaulturl1.com/bla-bla/", "http://defaulturl.com/bla-bla/", "http://defaulturl.ru/bla-bla/"]
        self.assertEqual(s, indexer.read_sources())

    def test_add_urls_2(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist2.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        indexer.add_urls("ftp://defaulturl1.com/bla-bla/")
        s = ["ftp://defaulturl1.com/bla-bla/"]
        self.assertEqual(s, indexer.read_sources())

    def test_add_urls_3(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist2.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        indexer.add_urls("ftp://defaulturl1.com/bla-bla/, ftp://defaulturl1.ru/bla-bla/")
        s = ["ftp://defaulturl1.com/bla-bla/", "ftp://defaulturl1.ru/bla-bla/"]
        self.assertEqual(s, indexer.read_sources())

    def test_add_urls_4(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        shutil.copy(os.path.abspath("./testdata/sourcelist1.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        indexer.add_urls("ftp://defaulturl.ru/bla-bla/")
        s = ["ftp://defaulturl.ru/bla-bla/", "http://defaulturl.com/bla-bla/", "http://defaulturl.ru/bla-bla/"]
        self.assertEqual(s, indexer.read_sources())
        
    def test_update_1(self):
        self.assertFalse(os.path.exists(os.path.join(self.work, "packetlist.yaml")))
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        indexer.update()
        self.assertTrue(os.path.exists(os.path.join(self.work, "packetlist.yaml")))

    def test_update_2(self):
        self.assertFalse(os.path.exists(os.path.join(self.work, "packetlist.yaml")))
        self.__make_workdir()
        
        indexer = Indexer(self.cache, self.work, MockFetcher1, verification = False)
        indexer.update()
        self.assertTrue(os.path.exists(os.path.join(self.work, "packetlist.yaml")))
        pl = PacketList()
        pl.load(os.path.join(self.work, "packetlist.yaml"))
        self.assertEqual("packet1", pl.packets[0].name)
        self.assertEqual("1.0.0.0", pl.packets[0].version)
        self.assertEqual(["url1/packet1/1.0.0.0"], pl.packets[0].urls)
        self.assertEqual("packet2", pl.packets[1].name)
        self.assertEqual("1.0.0.0", pl.packets[1].version)
        self.assertEqual(["url1/packet2/1.0.0.0"], pl.packets[1].urls)
        self.assertEqual("packet2", pl.packets[2].name)
        self.assertEqual("1.0.1.0", pl.packets[2].version)
        self.assertEqual(["url1/packet2/1.0.1.0", "url2/packet2/1.0.1.0"], pl.packets[2].urls)
        self.assertEqual("packet3", pl.packets[3].name)
        self.assertEqual("1.0.9.0", pl.packets[3].version)
        self.assertEqual(["url2/packet3/1.0.9.0"], pl.packets[3].urls)

        
    def test_get_version_1(self):
        self.__make_workdir()
        
        indexer = Indexer(self.cache, self.work, MockFetcher1, verification = False)
        indexer.update()
        
        self.assertEqual(["1.0.0.0"], indexer.get_versions("packet1"))
        self.assertEqual(["1.0.0.0", "1.0.1.0"], indexer.get_versions("packet2"))
        
    def test_download_1(self):
        self.__make_workdir1()
        indexer = Indexer(self.cache, self.work, MockFetcher1, verification = False)
        indexer.update()

        download_file1 = indexer.download("packet2")
        self.assertTrue(os.path.exists(download_file1))
        self.assertEqual("packet2_v1.0.1.0.zip.ori", os.path.basename(download_file1))
        
        download_file2 = indexer.download("packet2", "1.0.0.0")
        self.assertTrue(os.path.exists(download_file2))
        self.assertEqual("packet2_v1.0.0.0.zip.ori", os.path.basename(download_file2))
        
        download_file3 = indexer.download("packet3")
        self.assertTrue(os.path.exists(download_file3))
        self.assertEqual("packet3_v1.0.10.0.zip.ori", os.path.basename(download_file3))
        
        self.assertRaises(DownloadError, indexer.download, "packet4")
        
    def test_get_path(self):
        indexer = Indexer(self.cache, self.work, MockFetcher, verification = False)
        self.assertEqual(os.path.abspath(self.work), indexer.get_output_path())
        
    def test_get_packet_list(self):
        self.assertFalse(os.path.exists(os.path.join(self.work, "packetlist.yaml")))
        self.__make_workdir()
        
        indexer = Indexer(self.cache, self.work, MockFetcher1, verification = False)
        indexer.update()
        self.assertTrue(os.path.exists(os.path.join(self.work, "packetlist.yaml")))
        self.assertEqual(['packet1-1.0.0.0', 'packet2-1.0.0.0', 'packet2-1.0.1.0', 'packet3-1.0.9.0', 'packet3-1.0.10.0', 'packet4-1.0.0.0'], indexer.get_packet_list())
        
    def test_signed(self):
        url = "./testdata/work/url"
        os.makedirs(url)
        shutil.copy(os.path.abspath("testdata/signed_data/packetlist.yaml"), os.path.abspath(os.path.join(url, "packetlist.yaml")))
        shutil.copy(os.path.abspath("testdata/signed_data/packetlist.yaml.asc"), os.path.abspath(os.path.join(url, "packetlist.yaml.asc")))
        packet_url = "./testdata/work/url/test_packet/1.0.10.0"
        os.makedirs(packet_url)
        shutil.copy(os.path.abspath("testdata/signed_data/test_packet-1.0.10.0.zip.ori"), os.path.abspath(os.path.join(packet_url, "test_packet-1.0.10.0.zip.ori")))
        shutil.copy(os.path.abspath("testdata/signed_data/test_packet-1.0.10.0.zip.ori.asc"), os.path.abspath(os.path.join(packet_url, "test_packet-1.0.10.0.zip.ori.asc")))
        
        crypto = Cryptography(keyring = os.path.abspath("./testdata/test_keyring"))
        crypto.import_public_key("./testdata/signed_data/test_oriolus_key.asc")
        indexer = Indexer(self.cache, self.work, MockFetcher1, verification = True, keyring = os.path.abspath("./testdata/test_keyring"))
        indexer.update("./testdata/work/url/packetlist.yaml")
        download_file1 = indexer.download("test_packet")
        self.assertTrue(os.path.exists(download_file1))
        self.assertEqual("test_packet-1.0.10.0.zip.ori", os.path.basename(download_file1))
               
    def __make_workdir(self):
        url1 = "./testdata/work/url1"
        url2 = "./testdata/work/url2"
        url3 = "./testdata/work/url3"
        shutil.copy(os.path.abspath("testdata/sourcelist4.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        os.makedirs(url1)
        shutil.copy(os.path.abspath("./testdata/packetlist1.yaml"), os.path.abspath(os.path.join(url1, "packetlist.yaml")))
        os.makedirs(url2)
        shutil.copy(os.path.abspath("./testdata/packetlist2.yaml"), os.path.abspath(os.path.join(url2, "packetlist.yaml")))
        os.makedirs(url3)
        shutil.copy(os.path.abspath("./testdata/packetlist3.yaml"), os.path.abspath(os.path.join(url3, "packetlist.yaml")))

    def __make_workdir1(self):
        url1 = "./testdata/work/url1"
        url2 = "./testdata/work/url2"
        url3 = "./testdata/work/url3"
        url110 = "./testdata/work/url1/packet1/1.0.0.0"
        url120 = "./testdata/work/url1/packet2/1.0.0.0"
        url121 = "./testdata/work/url1/packet2/1.0.1.0"
        url221 = "./testdata/work/url2/packet2/1.0.1.0"
        url239 = "./testdata/work/url2/packet3/1.0.9.0"
        url2310 = "./testdata/work/url2/packet3/1.0.10.0"
        url3310 = "./testdata/work/url3/packet3/1.0.10.0"
        url340 = "./testdata/work/url3/packet4/1.0.0.0"
        os.makedirs(url1)
        os.makedirs(url2)
        os.makedirs(url3)
        os.makedirs(url110)
        os.makedirs(url120)
        os.makedirs(url121)
        os.makedirs(url221)
        os.makedirs(url239)
        os.makedirs(url2310)
        os.makedirs(url3310)
        os.makedirs(url340)
        
        shutil.copy(os.path.abspath("testdata/sourcelist4.yaml"), os.path.abspath(os.path.join(self.work, "sourcelist.yaml")))
        shutil.copy(os.path.abspath("./testdata/packetlist11.yaml"), os.path.abspath(os.path.join(url1, "packetlist.yaml")))
        shutil.copy(os.path.abspath("./testdata/packetlist12.yaml"), os.path.abspath(os.path.join(url2, "packetlist.yaml")))
        shutil.copy(os.path.abspath("./testdata/packetlist13.yaml"), os.path.abspath(os.path.join(url3, "packetlist.yaml")))
        
        shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url110, "packet1_v1.0.0.0.zip.ori")))
        shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url120, "packet2_v1.0.0.0.zip.ori")))
        #shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url121, "packet2_v1.0.1.0.zip.ori")))
        shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url221, "packet2_v1.0.1.0.zip.ori")))
        shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url239, "packet3_v1.0.9.0.zip.ori")))
        shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url2310, "packet3_v1.0.10.0.zip.ori")))
        shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url3310, "packet3_v1.0.10.0.zip.ori")))
        #shutil.copy(os.path.abspath("./testdata/packet.zip.ori"), os.path.abspath(os.path.join(url340, "packet4_v1.0.0.0.zip.ori")))
        
class TestPacketDescriptor(TestCase):
    def test_creation(self):
        pd1 = PacketDescriptor("packet1", "1.0.0.0", ["url1/packet1-1.0.0.0.zip.ori"])
        self.assertEqual("packet1", pd1.name)
        self.assertEqual("1.0.0.0", pd1.version)
        self.assertEqual(["url1/packet1-1.0.0.0.zip.ori"], pd1.urls)

    def test_compare_1(self):
        pd1 = PacketDescriptor("packet1", "1.0.0.0", ["url1/packet1-1.0.0.0.zip.ori"])
        pd2 = PacketDescriptor("packet1", "1.0.0.0", ["url2/packet1-1.0.0.0.zip.ori"])
        self.assertTrue(pd1 == pd2)

    def test_compare_2(self):
        pd1 = PacketDescriptor("packet1", "1.0.0.0", ["url1/packet1-1.0.0.0.zip.ori"])
        pd2 = PacketDescriptor("packet1", "1.1.0.0", ["url2/packet1-1.1.0.0.zip.ori"])
        self.assertFalse(pd1 == pd2)

    def test_compare_3(self):
        pd1 = PacketDescriptor("packet1", "1.0.0.0", ["url1/packet1-1.0.0.0.zip.ori"])
        pd2 = PacketDescriptor("packet2", "1.0.0.0", ["url1/packet2-1.0.0.0.zip.ori"])
        self.assertFalse(pd1 == pd2)

    def test_merge_1(self):
        pd1 = PacketDescriptor("packet1", "1.0.0.0", ["url1/packet1-1.0.0.0.zip.ori"])
        pd2 = PacketDescriptor("packet1", "1.0.0.0", ["url2/packet1-1.0.0.0.zip.ori"])
        pd3 = pd1.merge(pd2)

        self.assertFalse(pd3 == None)
        self.assertEqual("packet1", pd3.name)
        self.assertEqual("1.0.0.0", pd3.version)
        self.assertEqual(["url1/packet1-1.0.0.0.zip.ori", "url2/packet1-1.0.0.0.zip.ori"], pd3.urls)

    def test_merge_2(self):
        pd1 = PacketDescriptor("packet1", "1.0.0.0", ["url1/packet1-1.0.0.0.zip.ori", "url2/packet1-1.0.0.0.zip.ori"])
        pd2 = PacketDescriptor("packet1", "1.0.0.0", ["url2/packet1-1.0.0.0.zip.ori", "url3/packet1-1.0.0.0.zip.ori"])
        pd3 = pd1.merge(pd2)

        self.assertFalse(pd3 == None)
        self.assertEqual("packet1", pd3.name)
        self.assertEqual("1.0.0.0", pd3.version)
        self.assertEqual(["url1/packet1-1.0.0.0.zip.ori", "url2/packet1-1.0.0.0.zip.ori", "url3/packet1-1.0.0.0.zip.ori"], pd3.urls)

    def test_hash_1(self):
        pd1 = PacketDescriptor("packet1", "1.0.0.0", ["url1/packet1-1.0.0.0.zip.ori", "url2/packet1-1.0.0.0.zip.ori"])
        pd2 = PacketDescriptor("packet2", "1.0.0.0", ["url1/packet2-1.0.0.0.zip.ori", "url2/packet2-1.0.0.0.zip.ori"])
        pd3 = PacketDescriptor("packet1", "1.0.0.0", ["url2/packet1-1.0.0.0.zip.ori", "url3/packet1-1.0.0.0.zip.ori"])
        list = []
        list.append(pd1)
        list.append(pd2)
        self.assertTrue(pd3 in list)

class TestPacketList(TestCommon):
    def setUp(self):
        TestCommon.setUp(self)

    def tearDown(self):
        TestCommon.tearDown(self)

    def test_load_1(self):
        pl = PacketList()
        pl.load("./testdata/packetlist1.yaml")
        self.assertEqual(3, len(pl.packets))
        self.assertEqual("packet1", pl.packets[0].name)
        self.assertEqual("1.0.0.0", pl.packets[0].version)
        self.assertEqual(["url1/packet1/1.0.0.0"], pl.packets[0].urls)
        self.assertEqual("packet2", pl.packets[1].name)
        self.assertEqual("1.0.0.0", pl.packets[1].version)
        self.assertEqual(["url1/packet2/1.0.0.0"], pl.packets[1].urls)
        self.assertEqual("packet2", pl.packets[2].name)
        self.assertEqual("1.0.1.0", pl.packets[2].version)
        self.assertEqual(["url1/packet2/1.0.1.0"], pl.packets[2].urls)

    def test_load_2(self):
        pl = PacketList()
        pl.load("./testdata/packetlist4.yaml")
        self.assertEqual(0, len(pl.packets))

    def test_load_3(self):
        pl = PacketList()

        pl.load("./testdata/packetlist1.yaml")
        self.assertEqual(3, len(pl.packets))
        self.assertEqual(1, len(pl.packets[0].urls))
        self.assertEqual(1, len(pl.packets[1].urls))
        self.assertEqual(1, len(pl.packets[2].urls))

        pl.load("./testdata/packetlist2.yaml")
        self.assertEqual(5, len(pl.packets))
        self.assertEqual(1, len(pl.packets[0].urls))
        self.assertEqual(1, len(pl.packets[1].urls))
        self.assertEqual(2, len(pl.packets[2].urls))
        self.assertEqual(1, len(pl.packets[3].urls))
        self.assertEqual(1, len(pl.packets[4].urls))

        pl.load("./testdata/packetlist3.yaml")
        self.assertEqual(6, len(pl.packets))
        self.assertEqual(1, len(pl.packets[0].urls))
        self.assertEqual(1, len(pl.packets[1].urls))
        self.assertEqual(2, len(pl.packets[2].urls))
        self.assertEqual(1, len(pl.packets[3].urls))
        self.assertEqual(2, len(pl.packets[4].urls))
        self.assertEqual(1, len(pl.packets[5].urls))

        pl.load("./testdata/packetlist4.yaml")
        self.assertEqual(6, len(pl.packets))

        self.assertEqual("packet1", pl.packets[0].name)
        self.assertEqual("1.0.0.0", pl.packets[0].version)
        self.assertEqual(["url1/packet1/1.0.0.0"], pl.packets[0].urls)
        self.assertEqual("packet2", pl.packets[1].name)
        self.assertEqual("1.0.0.0", pl.packets[1].version)
        self.assertEqual(["url1/packet2/1.0.0.0"], pl.packets[1].urls)
        self.assertEqual("packet2", pl.packets[2].name)
        self.assertEqual("1.0.1.0", pl.packets[2].version)
        self.assertEqual(["url1/packet2/1.0.1.0", "url2/packet2/1.0.1.0"], pl.packets[2].urls)
        self.assertEqual("packet3", pl.packets[3].name)
        self.assertEqual("1.0.9.0", pl.packets[3].version)
        self.assertEqual(["url2/packet3/1.0.9.0"], pl.packets[3].urls)
        self.assertEqual("packet3", pl.packets[4].name)
        self.assertEqual("1.0.10.0", pl.packets[4].version)
        self.assertEqual(["url2/packet3/1.0.10.0", "url3/packet3/1.0.10.0"], pl.packets[4].urls)
        self.assertEqual("packet4", pl.packets[5].name)
        self.assertEqual("1.0.0.0", pl.packets[5].version)
        self.assertEqual(["url3/packet4/1.0.0.0"], pl.packets[5].urls)

    def test_sort1(self):
        pl = PacketList()
        pl.load("./testdata/packetlist2.yaml")

        self.assertEqual("1.0.9.0", str(pl.packets[1].version))
        self.assertEqual("1.0.10.0", str(pl.packets[2].version))

    def test_sort2(self):
        pl = PacketList()
        pl.load("./testdata/packetlist5.yaml")

        self.assertEqual("1.4.8.365", str(pl.packets[0].version))
        self.assertEqual("1.5.10.4523", str(pl.packets[1].version))
        self.assertEqual("1.5.11.4672", str(pl.packets[2].version))

    def test_save1(self):
        pl = PacketList()
        pl.load("./testdata/packetlist1.yaml")
        pl.load("./testdata/packetlist2.yaml")

        filename = os.path.join(self.work, "xxx.yaml")
        pl.save(filename)

        self.assertTrue(os.path.exists(filename))

    def test_get_packet_1(self):
        pl = PacketList()

        pl.load("./testdata/packetlist1.yaml")
        pl.load("./testdata/packetlist2.yaml")
        pl.load("./testdata/packetlist3.yaml")

        packet = pl.get_packet("packet2", "1.0.1.0")
        self.assertEqual("packet2", packet.name)
        self.assertEqual("1.0.1.0", packet.version)
        self.assertEqual(["url1/packet2/1.0.1.0", "url2/packet2/1.0.1.0"], packet.urls)

    def test_get_packet_2(self):
        pl = PacketList()

        pl.load("./testdata/packetlist1.yaml")
        pl.load("./testdata/packetlist2.yaml")
        pl.load("./testdata/packetlist3.yaml")

        packet = pl.get_packet("packet2")
        self.assertEqual("packet2", packet.name)
        self.assertEqual("1.0.1.0", packet.version)
        self.assertEqual(["url1/packet2/1.0.1.0", "url2/packet2/1.0.1.0"], packet.urls)

    def test_get_packet_3(self):
        pl = PacketList()

        pl.load("./testdata/packetlist1.yaml")
        pl.load("./testdata/packetlist2.yaml")
        pl.load("./testdata/packetlist3.yaml")

        packet = pl.get_packet("packet5")
        self.assertEqual(None, packet)

    def test_get_packet_4(self):
        pl = PacketList()

        packet = pl.get_packet("packet1")
        self.assertEqual(None, packet)

if __name__ == "__main__":
    suite1 = TestLoader().loadTestsFromTestCase(TestDownloader)
    suite2 = TestLoader().loadTestsFromTestCase(TestIndexer)
    suite3 = TestLoader().loadTestsFromTestCase(TestPacketDescriptor)
    suite4 = TestLoader().loadTestsFromTestCase(TestPacketList)
    alltests = TestSuite([suite1, suite2, suite3, suite4])
    TextTestRunner(verbosity=2).run(alltests)